package com.iptv.grape.video;

import java.util.concurrent.atomic.AtomicBoolean;

public class WorkThread implements Runnable {

    private IWork task;
    private Thread worker;
    private Object threadArgs;
    private final AtomicBoolean running = new AtomicBoolean(false);
    private int interval;

    public interface IWork{
        void doWork(Object args);
    }

    public WorkThread(int sleepInterval,IWork task) {
        this.interval = sleepInterval;
        this.task = task;
    }

    public void start() {
        worker = new Thread(this);
        worker.start();
    }

    public void stop() {
        running.set(false);
    }


    public void interrupt() {
        running.set(false);
        worker.interrupt();
    }

    public boolean isRunning() {
        return running.get();
    }






    @Override
    public void run() {
        running.set(true);
        while (running.get()) {
            try {
                Thread.sleep(interval);
                try {
                    task.doWork(this.threadArgs);
                }
                catch (Exception e){

                }
            } catch (InterruptedException e){
                Thread.currentThread().interrupt();
            }
        }
    }

}
